﻿using System;
using System.Windows.Forms;
using iTunesLib;
using NAudio.CoreAudioApi;

namespace iTunesHead
{
	public partial class Config : Form
	{
		#region [ Constants ]

		private const string HEAD_PHONE_GUID = "46d16a2c-5654-41c0-911e-7860d2bce7ee";
		private const string HEAD_PHONE_PLUGGED_VALUE = "1";
		private const string SONG_INFO_PATTERN = "[{0}] {1} - {2}";

		#endregion

		#region [ Fields ]

		iTunesApp iTunesApp;
		bool DoClose = false;

		#endregion

		#region [ Properties ]

		public bool IsHeadphonePlugged { get; set; }
		public bool ShowBaloons { get; set; }

		#endregion

		#region [ Constructor ]

		public Config()
		{
			InitializeComponent();
		}

		#endregion

		#region [ Events ]

		private void Config_Load(object sender, EventArgs e)
		{
			if (!IsiTuneOk())
				return;

			iTunesApp = new iTunesApp();
		}

		private void HeadphoneTimer_Tick(object sender, EventArgs e)
		{
			CheckHeadphone();

			if (!IsiTuneOk())
				return;

			try
			{
				if (iTunesApp.PlayerState == ITPlayerState.ITPlayerStatePlaying)
				{
					if (!IsHeadphonePlugged && PauseCheckBox.Checked)
					{
						iTunesApp.Pause();
						if (ShowBaloons) TrayNotify.ShowBalloonTip(2000, "iTunesHead", "Headphone unplugged!", ToolTipIcon.Warning);
					}
				}
				else
				{
					if (IsHeadphonePlugged && PlayCheckBox.Checked)
					{
						iTunesApp.Play();
						if (ShowBaloons) TrayNotify.ShowBalloonTip(2000, "iTunesHead", "Headphone plugged", ToolTipIcon.Info);
					}
				}
			}
			catch
			{
				SongInfoLabel.Text = "Error getting iTunes data...";
			}
		}

		private void SongInfoTimer_Tick(object sender, EventArgs e)
		{
			SongInfoLabel.Text = "";

			if (!IsiTuneOk())
			{
				SongInfoLabel.Text = "iTunes not found! (Maybe closed?)";
				return;
			}

			try
			{
				if (iTunesApp.PlayerState == ITPlayerState.ITPlayerStatePlaying)
				{
					TimeSpan songPosition = new TimeSpan(0, 0, iTunesApp.PlayerPosition);
					SongInfoLabel.Text = string.Format(SONG_INFO_PATTERN, songPosition.ToString(), iTunesApp.CurrentTrack.Artist, iTunesApp.CurrentTrack.Name);
					ToolTip.SetToolTip(SongInfoLabel, SongInfoLabel.Text);
				}
				else
				{
					SongInfoLabel.Text = "No Music?";
					if (PlayCheckBox.Checked)
						SongInfoLabel.Text += " Plug your headphone to play!";
					else
						SongInfoLabel.Text += " WHY?";
				}
			}
			catch
			{
				SongInfoLabel.Text = "Error getting iTunes data...";
			}
		}

		#endregion

		#region [ Helpers ]

		void CheckHeadphone()
		{
			var device = new MMDeviceEnumerator().GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
			if (device.Properties[new Guid(HEAD_PHONE_GUID)].Value.ToString() == HEAD_PHONE_PLUGGED_VALUE)
				IsHeadphonePlugged = true;
			else
				IsHeadphonePlugged = false;
		}

		bool IsiTuneOk()
		{
			try
			{
				if (iTunesApp == null)
					iTunesApp = new iTunesApp();

				var s = iTunesApp.PlayerState;
				return true;
			}
			catch
			{
				return false;
			}
		}

		#endregion

		private void configToolStripMenuItem_Click(object sender, EventArgs e)
		{
			// Show App
		}

		private void exitToolStripMenuItem_Click(object sender, EventArgs e)
		{
			DoClose = true;
			Application.Exit();
		}

		private void Config_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (!DoClose)
			{
				if (ShowBaloons) TrayNotify.ShowBalloonTip(2000, "iTunesHead", "iTunesHead is sill running", ToolTipIcon.Info);
				this.Hide();
				e.Cancel = true;
			}
		}

		private void TrayNotify_DoubleClick(object sender, EventArgs e)
		{
			// Show App
		}

		private void TrayNotify_Click(object sender, EventArgs e)
		{
			// Show App
		}

		private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
		{
			// Show About
		}

		private void LogoPictureBox_DoubleClick(object sender, EventArgs e)
		{
			// Show About
		}

	}
}
